package com.bn.feeds.tools;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.bn.feedMaster.WorkFlowTools;
import com.bn.feeds.catalog.configurations.CatalogFeedConfigurations;

import atg.deployment.server.Target;
import atg.epub.project.Project;

public class TruWorkFlowTools extends WorkFlowTools{
	
	private final static String PRODUCTION_TARGET = "Production";

	public String checkSanityTargets() {
        if (isLoggingInfo()) {
            logInfo("checkSanityTargets started");
        }
        String message = null;
        CatalogFeedConfigurations configs = null;//getCatalogFeedConfigurations();(Temporarly I commented Asha)
        // check if the process is disable
        if (!configs.isExecuteCheckSanityTargets()) {
            return message;
        }
        try {
            assumeUserIdentity(getUserName());
            Target productionTarget = getDeploymentServer().getTargetByName(PRODUCTION_TARGET);
            Project[] productionDeployedActiveProjects = productionTarget.getDeployedActiveProjects(0, -1);
            List<Project> productionProjectsToRevert = new ArrayList<Project>();
            if (productionDeployedActiveProjects != null) {
                productionProjectsToRevert.addAll(Arrays.asList(productionDeployedActiveProjects));
            }
            // get the queued projects on production
            List<Project> productionQueuedProjects = getActiveProjects(productionTarget);
            productionProjectsToRevert.removeAll(productionQueuedProjects);
            productionProjectsToRevert.addAll(productionQueuedProjects);
            // revert projects
            revertProjects(productionProjectsToRevert, PRODUCTION_TARGET);
            if (isLoggingInfo()) {
                logInfo("waiting for " + productionProjectsToRevert.size() + " projects to be reverted");
            }
            for (int i = 0; i < configs.getWaitTryNumber(); i++) {
                Thread.sleep((configs.getWaitTimeOutPerProjectSeconds() * 1000) * productionProjectsToRevert.size());
                if (productionTarget.getCountOfPendingDeployments() == 0 && productionTarget.getCountOfQueuedDeployments() == 0) {
                    if (isLoggingInfo()) {
                        logInfo("Revert process in production complete: " + i);
                    }
                    break;
                }
                if (isLoggingInfo()) {
                    logInfo("Wait try number: " + i);
                }
                if (i == configs.getWaitTryNumber() - 1) {
                    message = "Unable to revert all the projects from Production (timeout)";
                    return message;
                }
            }
            // COMPLETED REVERT PROJECTS FROM PRODUCTION
        } catch (final Exception e) {
            if (e.getMessage() != null) {
                message = e.getMessage();
            } else {
                message = "Critical Error. See logs for details: " + e.getCause();
            }
            if (isLoggingError()) {
                logError(e);
            }
        } finally {
            if (isLoggingInfo()) {
                logInfo("checkSanityTargets result: " + message);
            }
        }
        return message;
    }
	
	
}
